package com.cn.wanxi.servlet;

import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/userRegister")
public class UserRegisterServlet extends HttpServlet {
    /**
     * doPost 和doGet的区别
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        0.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        resp.setContentType("UTF-8");
//        1.得到页面传递过来的值

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        hobby = hobby.replace("[", "").replace("]", "").replace(" ", "");
        String introduce = req.getParameter("introduce");

//        2.需要将值封装到实体类
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setCode(code);
        userModel.setPhone(phone);
        userModel.setSex(sex);
        userModel.setBirthday(birthday);
        userModel.setHobby(hobby.replace("\"", ""));
        userModel.setIntroduce(introduce);
//        3.服务逻辑层实例化service
        IUserService userService = new UserServiceImpl();
//        调用，得到页面需要的数据
        int result = userService.add(userModel);
//        4.将数据传递到页面
//        req.setAttribute("result", result);
//        页面转发
        resp.getWriter().println(result);
//        req.getRequestDispatcher("/back/userRegister.jsp").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(1111);
    }


}
